<?php
namespace News\Controller;
use Think\Controller;
/**
 * 消息控制器 MessageController.class.php
 */
class MessageController extends CommonController{
   //方法声明
   public function __construct() {
        parent::__construct();
   }
   
   /**
    * 展示消息
    */
   public function showContent(){
       $data = I('get.');
       //提取用户userid
       $userid_name = $this -> corpid . '_userid';

       if(!$userid = session($userid_name)){
            $userid = $this -> getUserid($data);
       }
       //提取消息信息
       $news_id = explode('-',$data['state'])[1];
       $GLOBALS['redis'] -> select(1);
       $content = unserialize($GLOBALS['redis'] -> hget($this->msg_db_name,$news_id));
       //检测该消息是否已被清除
       if(!$content){
           $this->waitSecond = 2;
           $this -> error('该消息已被移除 <span style="color:red;">点此返回</span>');
       }
       //提取发送消息人信息
       $send_from  = explode('_',$news_id)[0];
       $send_from = $this -> getSendUserInfo($send_from);
       //提取消息时间
       $time = date('Y年m月d日 H:i:s',end(explode('_',$news_id)));
       //检测该用户是是否已经阅读
       $GLOBALS['redis'] -> select(2);
       $is_read = false;
       if($GLOBALS['redis'] -> scontains($this->corpid.'_'.$news_id,$userid)){
           $is_read = true;
       }
       $this -> assign('content',$content);
       $this -> assign('time',$time);
       $this -> assign('news_id',$news_id);
       $this -> assign('userid',$userid);
       $this -> assign('is_read',$is_read);
       $this -> assign('send_from',$send_from);
       $this -> assign('corpid',$this->corpid);
       $this -> display();
   }
   
   /**
    * 获取消息发送者信息
    */
   public function getSendUserInfo($userid){
       $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={$this->token}&userid=$userid";
       $info = $this -> https_curl($url);
       return json_decode($info,true)['name'];
   }
   
   /**
   * 获取用户userid
   */
   public function getUserid($data){
       $code = $data['code'];
       if(!$code){
           $this -> error('未经授权用户!');
           exit;
       }
       $access_token = $this -> token;
       $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$access_token}&code={$code}";
       $res = json_decode($this -> https_curl($url),true);
       if(!$userid = $res['UserId']){
           $this -> error('未经授权用户!');
       }
       session($userid_name,$userid);
       return $userid;
   }
   
   /**
    * 消息回执处理
    */
   public function receipt(){
       if(!IS_AJAX){
           return false;
       }
       $data = I('post.');
       $news_id = $data['news_id'];
       $user_id = $data['user_id'];
       $read_for_time = $data['read_time'];
       $read_time = time();
       $GLOBALS['redis'] -> select(1);
       //存储阅读人信息
       if(!$GLOBALS['redis'] -> hexists($this->msg_db_name,$news_id)){
           //消息已经不存在 不在做任何统计
           $this -> ajaxReturn(0);
       }
       $GLOBALS['redis'] -> select(2);
       //存储状态信息
       $key = $news_id.'-status';
       //数据结构 userid 读取用时 读取时间
       $value = $user_id.'-'.$read_for_time.'-'.$read_time;
       $GLOBALS['redis'] -> sadd($this->corpid.'_'.$key,$value);
       if($GLOBALS['redis'] -> sadd($this->corpid.'_'.$news_id,$user_id)){
           $this -> ajaxReturn(1);
       }else{
           $this -> ajaxReturn(0);
       }
   }
}
